package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.common.result.JsonResult;
import cn.itsource.ymcc.common.result.PageList;
import cn.itsource.ymcc.domain.Login;
import cn.itsource.ymcc.dto.LoginDto;
import cn.itsource.ymcc.query.LoginQuery;
import cn.itsource.ymcc.service.ILoginService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    public ILoginService loginService;

    @PostMapping("/123")
    public String xxx(){
        return "懒总不要上课玩手机";
    }

    @PostMapping("/common")
    public JsonResult common(@RequestBody @Valid LoginDto loginDto){
        return loginService.common(loginDto);
    }

    /**
     * 获取对象
     */
    @PostMapping("/accountLogin")
    public JsonResult accountLogin(@RequestBody @Valid LoginDto loginDto){
        return JsonResult.success().setData(loginService.accountLogin(loginDto));
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody Login login){
        if(login.getId()!=null){
            loginService.updateById(login);
            return JsonResult.success();
        }else{
            loginService.save(login);
            return JsonResult.success().setData(login.getId());
        }
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        loginService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(loginService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(loginService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody LoginQuery query){
        Page<Login> page = new Page<Login>(query.getPageNo(), query.getPageSize());
        page = loginService.page(page);
        return JsonResult.success().setData(new PageList<Login>(page.getTotal(), page.getRecords()));
    }
}
